package com.sjsu.aws.primitve.client.groupmembership;

import java.util.List;
import com.sjsu.aws.groupmembership.GroupMemberShip;
import com.sjsu.aws.groupmembership.GroupMembershipServiceImpl;
import com.sjsu.aws.utils.TimeUtil;
import com.sjsu.aws.utils.UUIDGenerator;

/**
 * 
 * @author Pawar Deepali
 * 
 */
public class ClientGroupMembership {
	
	public static void main(String args[]) {
		ClientGroupMembership clientGroupMembership = new ClientGroupMembership();
		try {
			clientGroupMembership.joinGroup();
			clientGroupMembership.ListMembersOfGroup();
			clientGroupMembership.updateEphemeralTimeStamp();
			clientGroupMembership.disJoingroup();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void ListMembersOfGroup() throws Exception {
		GroupMembershipServiceImpl groupMembershipServiceImpl = new GroupMembershipServiceImpl();
		List<GroupMemberShip> groupMemberShipList = groupMembershipServiceImpl
		        .listGroupMembers();
		System.out.println("Count of Members" + groupMemberShipList.size());
	}
	
	public void joinGroup() throws Exception {
		printStatus("Member joining hardware group");
		long timeout = 1;
		GroupMemberShip g = new GroupMemberShip();
		g.setUuid(UUIDGenerator.getUUID());
		g.setGroupId("G13");
		g.setMemberId("CD");
		g.setEndTimestamp(TimeUtil.getEndTime(timeout));
		GroupMembershipServiceImpl groupMembershipServiceImpl = new GroupMembershipServiceImpl();
		groupMembershipServiceImpl.joinGroup(g);
		printStatus("laptop joined hardware group");
	}
	
	public void updateEphemeralTimeStamp() throws Exception {
		long timeout = 10000;
		GroupMemberShip g = new GroupMemberShip();
		g.setMemberId("Laptop");
		g.setEndTimestamp(TimeUtil.getEndTime(timeout));
		GroupMembershipServiceImpl groupMembershipServiceImpl = new GroupMembershipServiceImpl();
		groupMembershipServiceImpl.renewEphemeralMembership(g);
	}
	
	public void disJoingroup() throws Exception {
		printStatus("DisJoining member java");
		GroupMemberShip g = new GroupMemberShip();
		g.setGroupId("G11");
		g.setMemberId("Laptop");
		GroupMembershipServiceImpl groupMembershipServiceImpl = new GroupMembershipServiceImpl();
		groupMembershipServiceImpl.disjoinGroup(g);
		printStatus("DisJoined member java from software");
	}
	
	private static void printStatus(String s) {
		System.out.println(s);
	}
}
